

package com.groep21.android;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class PhotoDbAdapter {

	public static final String KEY_LENGTH = "length";
	public static final String KEY_WEIGHT = "weight";
	public static final String KEY_PHOTODATE = "photodate";
	public static final String KEY_DESCRIPTION = "description";
	public static final String KEY_IMGURI = "imguri";
	public static final String KEY_ROWID = "_id";

	private static final String TAG = "PhotoDbAdapter";
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;

	/**
	 * Database creation sql statement
	 */
	private static final String DATABASE_CREATE = "create table photos (_id integer primary key autoincrement, "
			+ "length float, weight float, photodate text not null, description text, imguri text not null);";

	private static final String DATABASE_NAME = "data";
	private static final String DATABASE_TABLE = "photos";
	private static final int DATABASE_VERSION = 2;

	private final Context mCtx;

	private static class DatabaseHelper extends SQLiteOpenHelper {

		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {

			db.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
					+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS notes");
			onCreate(db);
		}
	}

	public PhotoDbAdapter(Context ctx) {
		this.mCtx = ctx;
	}

	public PhotoDbAdapter open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}

	public void close() {
		mDbHelper.close();
	}

	public void createPhoto(float length, float weight, String description, String ImgFile) {

		String sqlquery = "INSERT INTO " + DATABASE_TABLE
				+ " (length, weight, photodate, description, imguri) values (" + length
				+ ", " + weight + ", datetime('now'), '" + description + "', '" + ImgFile +  "')";

		mDb.execSQL(sqlquery);
	}

	// public long createPhoto(String title, String body) {
	// ContentValues initialValues = new ContentValues();
	// initialValues.put(KEY_DESCRIPTION, title);
	// initialValues.put(KEY_PHOTODATE, body);
	//
	// return mDb.insert(DATABASE_TABLE, null, initialValues);
	// }

	public boolean deletePhoto(long rowId) {

		return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}

	public Cursor fetchAllPhotos() {

		return mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_LENGTH,
				KEY_WEIGHT, KEY_PHOTODATE, KEY_DESCRIPTION , KEY_IMGURI}, null, null, null,
				null, null);
	}

	public String fetchAllLengths() {
		
		String data = "";
		int colIndex;

		Cursor c =  mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_LENGTH,
				KEY_WEIGHT, KEY_PHOTODATE, KEY_DESCRIPTION , KEY_IMGURI}, null, null, null,
				null, null);
		colIndex = c.getColumnIndex(KEY_LENGTH);
		
		if (c.moveToFirst()) {
			do {
				data = data + c.getString(colIndex)+",";
			} while(c.moveToNext());
			
		}
		data = data.substring(0, data.length() - 1);
		
		return data;
	}
	
public String fetchAllWeights() {
		
		String data = "";
		int colIndex;

		Cursor c =  mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_LENGTH,
				KEY_WEIGHT, KEY_PHOTODATE, KEY_DESCRIPTION , KEY_IMGURI}, null, null, null,
				null, null);
		colIndex = c.getColumnIndex(KEY_WEIGHT);
		
		if (c.moveToFirst()) {
			do {
				data = data + c.getString(colIndex)+",";
			} while(c.moveToNext());
			
		}
		data = data.substring(0, data.length() - 1);
		
		return data;
	}
	
	
	public Cursor fetchPhoto(long rowId) throws SQLException {

		Cursor mCursor =

		mDb.query(true, DATABASE_TABLE, new String[] { KEY_ROWID, KEY_LENGTH,
				KEY_WEIGHT, KEY_PHOTODATE, KEY_DESCRIPTION, KEY_IMGURI }, KEY_ROWID + "="
				+ rowId, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;

	}

	public int lastPhotoId() throws SQLException {

		int mLastPhoto = 0;
		int colIndex = 0;

		Cursor mCursor =

		mDb.query(DATABASE_TABLE, new String[] { KEY_ROWID, KEY_LENGTH,
				KEY_WEIGHT, KEY_PHOTODATE, KEY_DESCRIPTION }, null, null, null,
				null, null);

		colIndex = mCursor.getColumnIndex(KEY_ROWID);

		if (mCursor != null && mCursor.moveToLast()) {
			
			Log.d(this.toString(), "Amount of records in DB:"
					+ mCursor.getCount());
			Log.d(this.toString(), "Cursor posistion:"
					+ mCursor.getPosition());

			mLastPhoto = mCursor.getInt(colIndex);
			Log.d(this.toString(),"mLastPhoto:"+mLastPhoto);
			return mLastPhoto;

		}
		return mLastPhoto;

	}

	public void updatePhoto(long rowId, float length, float weight, String description, String ImgFile) {
		String sqlquery = "UPDATE " + DATABASE_TABLE + " SET " + KEY_LENGTH
				+ " = " + length + ", " + KEY_WEIGHT + " = " + weight + ", "
				+ KEY_DESCRIPTION + " = '" + description + "', " + KEY_IMGURI + " = '" + ImgFile + "' WHERE "
				+ KEY_ROWID + " = " + rowId;

		mDb.execSQL(sqlquery);
	}
}
